package com.skysz.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class JabberServer {
	public static final int PORT = 9999;
	
	public static void main(String[] args) throws IOException {
		ServerSocket s = new ServerSocket(PORT);
		System.out.println("Started : " + s);
		
		try {
			Socket socket = s.accept();
			try {
				System.out.print("Connection accepted :" + socket);
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
				while( true ) {
					String str = in.readLine();
					if( str.equals("END")) break;
					System.out.println("Echoing: " + str);
					out.println(str);
				}
			}finally {
				System.out.println("closing...");
				socket.close();
			}
		}finally {
			s.close();
		}
		
	}
}
